﻿#region Using Declarations

using System.Collections.Generic;
using System.Data;
using MySql.Data.MySqlClient;
using Portfolio.Models;

#endregion

namespace Portfolio.Persistencia
{
    public class Eventos
    {
        #region Methods

        public static Evento BuscarEvento(int id)
        {
            Evento evento = null;

            using (MySqlConnection conn = DBContext.GetInstance())
            {
                MySqlCommand command = new MySqlCommand()
                {
                    Connection = conn,
                    CommandType = System.Data.CommandType.Text,
                    CommandText = string.Format("SELECT * FROM Eventos WHERE Id = {0}", id)
                };

                conn.Open();

                try
                {
                    MySqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);

                    command.Dispose();

                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            evento = new Evento()
                            {
                                Id = (int)reader["Id"],
                                IdCategoria = (int)reader["IdCategoria"],
                                NomeExibicao = reader["NomeExibicao"].ToString(),
                                NomeCover = Covers.BuscarCover((int)reader["Id"])
                            };
                        }
                    }

                    reader.Close();
                    reader.Dispose();
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    conn.Close();
                }
            }

            return evento;
        }

        public static IEnumerable<Evento> ListarEventos(int idCategoria)
        {
            List<Evento> eventos = null;

            using (MySqlConnection conn = DBContext.GetInstance())
            {
                MySqlCommand command = new MySqlCommand()
                {
                    Connection = conn,
                    CommandType = System.Data.CommandType.Text,
                    CommandText = string.Format("SELECT * FROM Eventos WHERE IdCategoria = {0}", idCategoria)
                };

                conn.Open();

                try
                {
                    MySqlDataReader reader = command.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

                    command.Dispose();

                    if (reader.HasRows)
                    {
                        eventos = new List<Evento>();

                        while (reader.Read())
                        {
                            eventos.Add(new Evento()
                            {
                                Id = (int)reader["Id"],
                                IdCategoria = (int)reader["IdCategoria"],
                                NomeExibicao = reader["NomeExibicao"].ToString(),
                                NomeCover = Covers.BuscarCover((int)reader["Id"])
                            });
                        }
                    }

                    reader.Close();
                    reader.Dispose();
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    conn.Close();
                }
            }

            return eventos;
        }

        #endregion
    }
}